﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluginInterfacesLib;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Threading;

namespace GeoPlugin
{
    public class GeoPlugin : IPlugin
    {
        private String name;
        private static IPluginHost host;
        public String Name { get { return this.name; } }
        public IPluginHost Host { get { return host; } set { host = value; } }
        public static int backButtonLevel;
        private static ObjectControl objectControl;
        private static Thread pluginModelThread;

        public GeoPlugin()
        {
            this.name = "GeoPlugin";
        }

        public void Initialize() 
        {
            objectControl = new ObjectControl();
            objectControl.Opacity = 0;
            PluginModel.Instance.ObjectControl = objectControl;
            host.AddOverlappingWindow(objectControl);
            host.AddButtonToPluginBar(4, "Back", "Back", HandleBackButton);           
            PluginModel.Instance.Host = host;
            pluginModelThread = new Thread(new ThreadStart(PluginModel.Instance.ThreadRun));
            PluginModel.Instance.RunThread = true;
            pluginModelThread.Name = "PluginModelThread started from Geoplugin.cs";
            pluginModelThread.Start();
        }

        public Bitmap GetPluginImage()
        {
            Stream s = this.GetType().Assembly.GetManifestResourceStream("GeoPlugin.1.jpg");
            return new Bitmap(s);
        }

        public void StopPlugin()
        {
            //Console.WriteLine("The plugin is supposed to stop now...");
            PluginModel.Instance.RunThread = false;
            objectControl.Dispatcher.BeginInvoke(new Action(delegate() { objectControl.Opacity = 0; }));

        }

        public void StartPlugin()
        {
            objectControl.Dispatcher.BeginInvoke(new Action(delegate(){objectControl.Opacity = 1;}));
            Console.WriteLine("The plugin is supposed to start now...");
            PluginModel.Instance.RunThread = true;
        }

        public static void AddBackButton()
        {
            backButtonLevel = 1;
        }

        public static void IncreaseBackButtonLevel()
        {
            backButtonLevel++;
        }

        public static void HandleBackButton(object sender, EventArgs e)
        {
            if (backButtonLevel > 0)
            {
                ((FrameworkElement)objectControl.MainGrid.Children[backButtonLevel+2]).Opacity = 0.7;
                for (int i = backButtonLevel+3; i < objectControl.MainGrid.Children.Count; i++)
                { 
                    objectControl.MainGrid.Children.RemoveAt(i);
                }
                backButtonLevel--;
            }
        }

        public void Dispose()
        {
            
        } 
    }
}
